import fs from "node:fs";
import path from "node:path";

async function getEntityFiles(dirPath) {
    let entityFiles = [];

    async function readDirRecursive(directory) {
        try {
            const files = await fs.readdirSync(directory, {withFileTypes: true});

            for (const file of files) {
                const fullPath = path.join(directory, file.name);

                if (file.isDirectory()) {
                    // 如果是目录，则递归读取
                    await readDirRecursive(fullPath);
                } else if (file.isFile() && (file.name.endsWith('.js') || file.name.endsWith('.ts'))) {
                    // 如果是文件且是 .js 或 .ts，则添加到实体文件数组中
                    entityFiles.push(fullPath);
                }
            }
        } catch (err) {
            console.error(`Error reading directory ${directory}:`, err);
        }
    }

    // 从初始目录开始递归读取
    await readDirRecursive(dirPath);

    return entityFiles;
}


// 使用
// 使用示例
// (async () => {
//     const dirPath = path.resolve(__dirname, '../Entity');
//     const files = await getEntityFiles(dirPath);
//     console.log(files); // 输出所有 .js 和 .ts 文件的完整路径
// })();
export default getEntityFiles